
import random
from flask import request
from . import app_user
from conn_mysql import MysqlApi

class SendSms:

    def __init__(self, phone):
        self.phone = phone
        self.verify_code = random.randint(100000, 999999)
        with MysqlApi() as m:
            m.insert_verify_code(self.phone, self.verify_code)
        self.send_sms_()

    def send_sms_(self):

        print(f"短信已经送，verify_code:{self.verify_code}")


@app_user.route('/send_sms', methods=['POST'])
def send_sms():

    phone = request.form.get("phone")
    with MysqlApi() as m:

        result = m.can_i_send_sms(phone)

    if not result:

        return {
                "status": -1,
                "errorCode": 1,
                "errorMsg": '你刚刚已经发送过一条短信',
                "resultBody": None,
            }

    SendSms(phone)#发送短信

    return {
                "status": 11,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "短信发送成功，请注意查收",
            }

    
